/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CBGUI.java
 *
 * Created on 14/09/2010, 13:48:46
 */
package jogamesuite;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import jogamesuite.lib.*;

/**
 *
 * @author Joaquin
 */
public class CBGUI extends javax.swing.JFrame {

    /** Creates new form CBGUI */
    public CBGUI() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.JPanel optionsPanel = new javax.swing.JPanel();
        javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
        forumType = new javax.swing.JComboBox();
        javax.swing.JLabel jLabel2 = new javax.swing.JLabel();
        backgroundColor = new javax.swing.JComboBox();
        javax.swing.JLabel jLabel3 = new javax.swing.JLabel();
        showTech = new javax.swing.JCheckBox();
        showCoords = new javax.swing.JCheckBox();
        javax.swing.JPanel inputPanel = new javax.swing.JPanel();
        javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
        inputText = new javax.swing.JTextArea();
        readButton = new javax.swing.JButton();
        javax.swing.JScrollPane jScrollPane2 = new javax.swing.JScrollPane();
        outputText = new javax.swing.JTextArea();
        clearButton = new javax.swing.JButton();
        copyAllButton = new javax.swing.JButton();
        javax.swing.JPanel previewPanel = new javax.swing.JPanel();
        javax.swing.JScrollPane jScrollPane3 = new javax.swing.JScrollPane();
        previewText = new javax.swing.JTextPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("jOgameSuite Compactador de Batallas");

        optionsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Opciones", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), java.awt.SystemColor.textText)); // NOI18N

        jLabel1.setText("Foro:");

        forumType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "phpBB", "HTML" }));

        jLabel2.setText("Fondo:");

        backgroundColor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Claro", "Oscuro" }));
        backgroundColor.setSelectedIndex(1);

        jLabel3.setText("Mostrar:");

        showTech.setSelected(true);
        showTech.setText("Tecnologías");

        showCoords.setText("Coordenadas");

        javax.swing.GroupLayout optionsPanelLayout = new javax.swing.GroupLayout(optionsPanel);
        optionsPanel.setLayout(optionsPanelLayout);
        optionsPanelLayout.setHorizontalGroup(
            optionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(optionsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(optionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(optionsPanelLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(forumType, 0, 175, Short.MAX_VALUE))
                    .addGroup(optionsPanelLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(backgroundColor, 0, 175, Short.MAX_VALUE))
                    .addGroup(optionsPanelLayout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(optionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(showCoords)
                            .addComponent(showTech))))
                .addContainerGap())
        );

        optionsPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel2, jLabel3});

        optionsPanelLayout.setVerticalGroup(
            optionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(optionsPanelLayout.createSequentialGroup()
                .addGroup(optionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(forumType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(optionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(backgroundColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(optionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(showTech))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(showCoords))
        );

        inputPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Copiar Reporte:", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), java.awt.SystemColor.textText)); // NOI18N

        inputText.setColumns(20);
        inputText.setFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N
        inputText.setRows(5);
        jScrollPane1.setViewportView(inputText);

        readButton.setAction(new ReadAction());
        readButton.setText("Leer");

        outputText.setColumns(20);
        outputText.setFont(new java.awt.Font("Monospaced", 0, 12));
        outputText.setRows(5);
        jScrollPane2.setViewportView(outputText);

        clearButton.setAction(new ClearAllAction());
        clearButton.setText("Borrar Todo");

        copyAllButton.setAction(new SelectAndCopyAction());
        copyAllButton.setText("Seleccionar todo y Copiar");

        javax.swing.GroupLayout inputPanelLayout = new javax.swing.GroupLayout(inputPanel);
        inputPanel.setLayout(inputPanelLayout);
        inputPanelLayout.setHorizontalGroup(
            inputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(inputPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(inputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(inputPanelLayout.createSequentialGroup()
                        .addComponent(readButton, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(clearButton, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                    .addComponent(copyAllButton, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE))
                .addContainerGap())
        );

        inputPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {clearButton, readButton});

        inputPanelLayout.setVerticalGroup(
            inputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(inputPanelLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(inputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(readButton)
                    .addComponent(clearButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(copyAllButton)
                .addContainerGap())
        );

        previewPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Vista Previa:", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), java.awt.SystemColor.textText)); // NOI18N

        jScrollPane3.setBorder(null);

        previewText.setBackground(java.awt.SystemColor.control);
        previewText.setBorder(null);
        previewText.setContentType("text/html");
        previewText.setEditable(false);
        jScrollPane3.setViewportView(previewText);

        javax.swing.GroupLayout previewPanelLayout = new javax.swing.GroupLayout(previewPanel);
        previewPanel.setLayout(previewPanelLayout);
        previewPanelLayout.setHorizontalGroup(
            previewPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(previewPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 382, Short.MAX_VALUE)
                .addContainerGap())
        );
        previewPanelLayout.setVerticalGroup(
            previewPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(previewPanelLayout.createSequentialGroup()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 587, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(optionsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(inputPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(previewPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(previewPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(optionsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(inputPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            // Set System L&F
            UIManager.setLookAndFeel(
                    UIManager.getSystemLookAndFeelClassName());
        } catch (UnsupportedLookAndFeelException e) {
            // handle exception
        } catch (ClassNotFoundException e) {
            // handle exception
        } catch (InstantiationException e) {
            // handle exception
        } catch (IllegalAccessException e) {
            // handle exception
        }
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CBGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox backgroundColor;
    private javax.swing.JButton clearButton;
    private javax.swing.JButton copyAllButton;
    private javax.swing.JComboBox forumType;
    private javax.swing.JTextArea inputText;
    private javax.swing.JTextArea outputText;
    private javax.swing.JTextPane previewText;
    private javax.swing.JButton readButton;
    private javax.swing.JCheckBox showCoords;
    private javax.swing.JCheckBox showTech;
    // End of variables declaration//GEN-END:variables

    private class ReadAction extends AbstractAction {

        public ReadAction() {
            super("Leer");
        }

        public void actionPerformed(ActionEvent evt) {
            CombatReportReader reader;
            try {
                reader = new CombatReportReader(inputText.getText());
            } catch (Exception ex) {
                ex.printStackTrace();
                outputText.setText("Error al leer\n" + ex.getMessage());
                return;
            }
            try {
                CombatReportWriter writer = new CombatReportWriter(reader, forumType.getSelectedIndex(), backgroundColor.getSelectedIndex());
                outputText.setText(writer.writeReport(forumType.getSelectedIndex(), showTech.isSelected(), showCoords.isSelected()));
                switch (backgroundColor.getSelectedIndex()) {
                    case ForumFormatter.COLOR_DARK:
                        previewText.setBackground(Color.DARK_GRAY);
                        previewText.setText("<font face = \"Tahoma\" color=\"#E6E6E6\" >"
                                + writer.writeReport(ForumFormatter.HTML_CODE, showTech.isSelected(), showCoords.isSelected())
                                + "<font>");
                        break;
                    case ForumFormatter.COLOR_LIGHT:
                        previewText.setBackground(Color.LIGHT_GRAY);
                        previewText.setText("<font face = \"Tahoma\" color=\"#0B0B0B\" >"
                                + writer.writeReport(ForumFormatter.HTML_CODE, showTech.isSelected(), showCoords.isSelected())
                                + "<font>");
                        break;
                    default:
                        previewText.setBackground(Color.DARK_GRAY);
                        previewText.setText("<font face = \"Tahoma\" color=\"#E6E6E6\" >"
                                + writer.writeReport(ForumFormatter.HTML_CODE, showTech.isSelected(), showCoords.isSelected())
                                + "<font>");
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                outputText.setText("Error al escribir\n" + ex.toString());
            }
        }
    }

    private class ClearAllAction extends AbstractAction {

        public ClearAllAction() {
            super("Borrar todo");
        }

        public void actionPerformed(ActionEvent e) {
            inputText.setText("");
            outputText.setText("");
            previewText.setText("");
        }
    }

    private class SelectAndCopyAction extends AbstractAction {

        public SelectAndCopyAction() {
            super("Seleccionar y copiar todo");
        }

        public void actionPerformed(ActionEvent e) {
            outputText.grabFocus();
            outputText.selectAll();
            outputText.copy();
        }
    }
}
